using NodeCanvas.DialogueTrees;
using UnityEngine;

namespace NodeCanvas.StateMachines
{
	[AddComponentMenu("")]
	[Name("Dialogue")]
	[Category("Nested")]
	[Description("Execute the assigned Dialogue Tree OnEnter and stop it OnExit. Optionaly an event can be send for whether the dialogue ended in Success or Failure.\nThis can be controled by using the 'End' Dialogue Node inside the Dialogue Tree.\nUse a 'CheckEvent' condition to make use of those events.")]
	public class FSMNestedDLGState : FSMState, INestedNode
	{
		[SerializeField]
		private DialogueTree _nestedDLG;

		[SerializeField]
		private string successEvent;

		[SerializeField]
		private string failureEvent;

		private DialogueTree nestedDLG
		{
			get
			{
				return _nestedDLG;
			}
			set
			{
				_nestedDLG = value;
			}
		}

		[HideInInspector]
		public Graph nestedGraph
		{
			get
			{
				return nestedDLG;
			}
			set
			{
				nestedDLG = (DialogueTree)value;
			}
		}

		protected override void Enter()
		{
			if (!nestedDLG)
			{
				Finish(false);
			}
			else
			{
				nestedDLG.StartGraph(base.graphAgent, base.graphBlackboard, OnDialogueFinished);
			}
		}

		private void OnDialogueFinished()
		{
			if (!string.IsNullOrEmpty(successEvent) && nestedDLG.endState == DialogueTree.EndState.Success)
			{
				SendEvent(successEvent);
			}
			if (!string.IsNullOrEmpty(failureEvent) && nestedDLG.endState == DialogueTree.EndState.Failure)
			{
				SendEvent(failureEvent);
			}
			Finish();
		}

		protected override void Exit()
		{
			if ((bool)nestedDLG && nestedDLG.isRunning)
			{
				nestedDLG.StopGraph();
			}
		}

		protected override void Pause()
		{
			if ((bool)nestedDLG)
			{
				nestedDLG.PauseGraph();
			}
		}
	}
}
